/*  
     PlAr is Platform Arena: a 2D multiplayer shooting game
     Copyright (C) 2009 Ragagnin Antonio <spocchio@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package plar.core;

import java.util.*;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


import plar.ClientServer.ShownElement;
import plar.core.defaultElements.Gun;
import plar.core.defaultElements.Player;
import plar.elements.*;

public class Game {

	public Integer clock;
	private int gamestate;
	public Parameters info;
	public Level level;
	private List<Player> players;
	private ArrayList <String> chat;
	public Point resolution;

	public Game() {
		players = new ArrayList<Player>();
		gamestate = 0;
		info = new Parameters();
		info.put("life", 100);
		resolution = new Point(800, 600);
		chat = new ArrayList <String>();
		for(int i =0;i<10;i++) chat.add("");

	}

	public void addPlayer(Player s) {
		players.add(s);
	}

	public List<Player> getPlayers(String s) {
		return players;
	}

	public ArrayList<ShownElement> getScreen(Player currentPlayer) {

		Rectangle screen = new Rectangle();

		screen.x = currentPlayer.getPosition().x - resolution.x / 2;
		screen.y = currentPlayer.getPosition().y - resolution.y / 2;
		//screen.y = currentPlayer.getPosition().y - (int)(((double)resolution.y) * (2.0 / 3.0));
		Point curpos = currentPlayer.getPosition();

		screen.width = resolution.x;
		screen.height = resolution.y;

		ArrayList<ShownElement> inside = new ArrayList<ShownElement>();
		HashMap<String, Object> refresh = new HashMap<String, Object>();
		refresh.put("subAction", "guiRefresh");

		for (Element e : level.elements) {

			Rectangle r = new Rectangle(e.getPosition().x, e.getPosition().y, e
					.getSize().x, e.getSize().y);
			if (screen.intersects(r)) {
				refresh.put("me", e);
				// Common.info("game : refreshing "+e);
				// e.actions.runAll(info);

				r.x = -curpos.x + r.x + resolution.x / 2;
				r.y = -curpos.y + r.y + resolution.y / 2;
				//r.y = -curpos.y + r.y + resolution.y * 2 / 3;

				inside.add(new ShownElement(r, e.getCurrentSpriteSet()));
			}
		}
		return inside;

	}

	public void respawn() {
		if (gamestate != 1)
			return;
		ArrayList<Element> respawns;
		respawns = level.getElementsByType("spawn");
		for (Element e : respawns) {
			if (!players.isEmpty()) {
				sendChat(players.get(0),"respawn!");
		players.get(0).setProperty("level",level);
		players.get(0).gl.clear();		
		players.get(0).gl.add(new GunQueen(players.get(0)));
		players.get(0).gl.add(new GunRage(players.get(0)));
		players.get(0).gl.add(new GunMine(players.get(0)));

		players.get(0).setProperty("currentGun", 0);
		
				level.AddElement(players.get(0));
				players.get(0).setPosition(e.getPosition());
				info.put("me", players.get(0));
				info.put("subAction", "new");
				
				players.get(0).actions.runAction("born", info);
				((ArrayList<Gun>) (players.get(0).getProperty("gunList"))).get(
						0).addAmmo(10);
			  	players.remove(0);
            
			}
		}

	}

	public void run() {
	
		if (gamestate != 1)
			return;

		// check who died and add it to the dead list
		List<Player> currentplayers = (ArrayList) level.getPlayers();
		for (Player p : currentplayers) {
			if ((Integer) p.getProperty("energy") <= 0) {
				players.add(p);
				level.delElement(p);
			}
		}

		// respawn who is dead
		respawn();

		//Thread tl = new Thread(level, "level");
		//tl.start();
		level.run();
		

	}

	public void sendInput(Player p, ArrayList<String> premuti) {
		Parameters input = new Parameters();
		input.put("key", premuti);
		input.put("me", p);
		input.put("level", level);
		input.put("subAction", "input");
		p.actions.runAction("input", input);

	}

	public void setLevel(Level l) {
		if (gamestate == 0)
			level = l;
		info.put("level", level);
	}

	public void sendChat(Player p, String s)
	{
		Calendar cal = new GregorianCalendar();
		 int ore = cal.get(Calendar.HOUR);
		int minuti = cal.get(Calendar.MINUTE);
		int secondi = cal.get(Calendar.SECOND);
		String nick = (String) p.getProperty("user");
		chat.add(chat.size()-1,"["+ore+":"+minuti+":"+secondi+"] "+nick+"> "+s);
		chat.remove(0);
	}
		public void sendChat(String s)
	{
		 Calendar cal = new GregorianCalendar();
		 int ore = cal.get(Calendar.HOUR);
		int minuti =cal.get(Calendar.MINUTE);
		int secondi = cal.get(Calendar.SECOND);

		chat.add(chat.size()-1,"["+ore+":"+minuti+":"+secondi+"] * "+s);
		chat.remove(0);
	}
	
			public String getChat(Player p)
	{
		String s=new String();
		int energy = (Integer) p.getProperty("energy");
		String name = (String) p.getProperty("user");
		 for(int i = 0;i<chat.size();i++) s+=chat.get(i)+"\n";
		 s+="\n"+name+" energy: "+energy;
		 int sel = p.getCurrentGun();
		 for(int i = 0;i<p.gl.size();i++)
		 {
			 Gun g = p.gl.get(i);
			 if(sel!=i)
			 s+="\n"+g.getGunName()+": "+g.getAmmo()+"ammo";
			 else
			 s+="\n["+g.getGunName()+": "+g.getAmmo()+" ammo]";
		 }
		 return s;
	}
	public void startGame() {
		if (gamestate != 0)
			return;
		gamestate++;
		respawn();

	}

}
